<?php

namespace app\index\controller;
use app\data\model\DataDistributionQueue;
use app\data\model\DataUserAccount;
use app\data\model\ShopOrder;
use app\data\model\DataUser;
use think\admin\Controller;
class Test extends Controller
{
    public function index()
    {
        $date = \Tools::getDateTimeByTime(time(),'d');
            if($date != '08'){
                return true;
            }
            //获取所有分销员
            $lists = DataDistributionQueue::mk()->where(['status'=>0])
                ->limit(0,100)
                ->select();
            $start_time = \Tools::getDateTimeByTime(strtotime("-1 months"),'Y-m').'-01 00:00:01';
            $end_time = \Tools::getDateTimeByTime(time(),'Y-m').'-01 00:00:01';

            $commissionModel = new \app\api\model\DataUserCommission();
            $commissionModel->startTrans();
            $opera = [];
            if(empty($lists) === FALSE){
                $lists = $lists->toArray();
                foreach($lists as &$lv){
                    $opera[] = DataDistributionQueue::mk()->where(['id'=>$lv['id']])->save(array(
                        'status'    =>1,
                        'update_time'=>\Tools::getDateTimeByTime()
                    ));
                    //获取分销员的直接分销员
                    $pre_child = DataUser::mk()->where(['pre_dis_id'=>$lv['user_id']])->column('id');
                    //获取分销员的间接分销员
                    $pre_pre_child = DataUser::mk()->where(['pre_pre_dis_id'=>$lv['user_id']])
                        ->column('id');
                    $forderAmount = 0;
                    if(empty($pre_child) === FALSE){
                        //获取对应分销员订单
                        $pre_child_arr = array_chunk($pre_child,50);
                        foreach($pre_child_arr as &$pfv){
                            $forderAmount = ShopOrder::mk()->whereIn('uuid',implode(',',$pfv))
                                ->where(array('status'=>6,'has_settle_account'=>0,'channel'=>0))
                                ->whereBetween('finish_datetime',$start_time.','.$end_time)
                                ->whereIn('payment_type','0,1')
                                ->sum('amount_total');
                            //更新订单为已结算
                            if($forderAmount > 0){
                                $opera[] = ShopOrder::mk()->whereIn('uuid',implode(',',$pfv))
                                ->where(array('status'=>6,'has_settle_account'=>0,'channel'=>0))
                                ->whereBetween('finish_datetime',$start_time.','.$end_time)
                                ->whereIn('payment_type','0,1')->save(array(
                                    'has_settle_account'    => 1,
                                    'update_at' => \Tools::getDateTimeByTime()
                                ));
                            }
                        }

                    }
                    $sorderAmount = 0;
                    if(empty($pre_pre_child) === FALSE){
                        $pre_pre_child_arr = array_chunk($pre_pre_child,50);
                        foreach($pre_pre_child_arr as &$psv1) {
                            //获取对应分销员订单
                            $sorderAmount = ShopOrder::mk()->whereIn('uuid', implode(',', $psv1))
                                ->where(array('status'=>6,'has_settle_account'=>0,'channel'=>0))
                                ->whereBetween('finish_datetime', $start_time . ',' . $end_time)
                                ->whereIn('payment_type', '0,1')
                                ->sum('amount_total');
                            if($sorderAmount > 0){
                                //更新订单为已结算
                                $opera[] = ShopOrder::mk()->whereIn('uuid', implode(',', $psv1))
                                    ->where(array('status'=>6,'has_settle_account'=>0,'channel'=>0))
                                    ->whereBetween('finish_datetime', $start_time . ',' . $end_time)
                                    ->whereIn('payment_type', '0,1')->save(array(
                                        'has_settle_account' => 1,
                                        'update_at' => \Tools::getDateTimeByTime()
                                    ));
                            }
                        }
                    }

                    //计算佣金比例
                    $res_amount = 0;
                    if($forderAmount > 0){
                        $res_amount = \Common::getLevel(1,$forderAmount);
                    }
                    if($res_amount > 0){
                        //更新佣金
                        $opera[] = DataUserAccount::mk()->where(['uuid' => $lv['user_id']])->inc('all_amount', $res_amount)->save();
                        $opera[] = DataUserAccount::mk()->where(['uuid' => $lv['user_id']])->inc('avail_amount', $res_amount)->save();
                        //添加佣金记录
                        $opera[] = $commissionModel->addData(array(
                            'uuid' => $lv['user_id'],
                            'type' => 0,
                            'created_at' => \Tools::getDateTimeByTime(),
                            'amount' => $forderAmount,
                            'commission_amount' => $res_amount,
                            'product_uuid' => 0,
                            'product_level' => 1,
                            'product_rate' => 0
                        ));
                    }
                    $res_sec_amount = 0;
                    if($sorderAmount > 0){
                        $res_sec_amount = \Common::getLevel(0,$sorderAmount);
                    }
                    if($res_sec_amount > 0){
                        //更新佣金
                        $opera[] = DataUserAccount::mk()->where(['uuid' => $lv['user_id']])->inc('all_amount', $res_sec_amount)->save();
                        $opera[] = DataUserAccount::mk()->where(['uuid' => $lv['user_id']])->inc('avail_amount', $res_sec_amount)->save();

                        $opera[] = $commissionModel->addData(array(
                            'uuid' => $lv['user_id'],
                            'type' => 0,
                            'created_at' => \Tools::getDateTimeByTime(),
                            'amount' => $sorderAmount,
                            'commission_amount' => $res_sec_amount,
                            'product_uuid' => 0,
                            'product_level' => 2,
                            'product_rate' => 0
                        ));
                    }

                    $opera[] = DataDistributionQueue::mk()->where(['id'=>$lv['id']])->save(array(
                        'status'    =>2,
                        'update_time'=>\Tools::getDateTimeByTime()
                    ));
                }
            }
            if(\Tools::checkArrayResult($opera)){
                $commissionModel->commit();
            }else{
                $commissionModel->rollBack();
                sysoplog('结算佣金失败','系统：结算佣金：失败');
                //TODO 发短信警告
            }
    }
}
